'''
Created on Dec 7, 2011

@author: jeongjin
'''
import os 
import random

#src_path = "./"
src_path = "/Users/jeongjin/Dropbox/224w/data/AgraphTestData/"

label_path = ""
label_file = None
feature_file_dct = {}

result_size = 500000
result_file_name = src_path+"data_"+str(result_size)+".csv"

for filename in os.listdir(src_path):
    if "label" in filename :
        label_path = src_path+filename
        label_file = open(label_path,'r')
    if "feature" in filename :
        feature_name = filename.split('.')[0].split('_')[1]
        feature_file_dct[feature_name] = open(src_path+filename,"r")

# get entire sets size
number_of_sets = int(os.popen("wc -l "+label_path).readlines()[0].split()[0])

# get result set row number list
rows = sorted(random.sample(range(0,number_of_sets),result_size))

result_file = open(result_file_name,'w')
# headline
headline = "node1,node2"
for feature_name in sorted(feature_file_dct.keys()):
    headline+=","
    headline+=feature_name
headline += ",label\n"
result_file.write(headline)

idx = 0
lines = {}
label_line = ""
while (len(rows)>0) :    
    if idx % 10000 == 0 : print idx    
    label_line = label_file.readline().strip()
    for feature_name in sorted(feature_file_dct.keys()):
        lines[feature_name] = feature_file_dct[feature_name].readline().strip()
    if idx == rows[0] :
        node1,node2,label = label_line.split('\t')
        result_line=node1+','+node2
        for feature_name in sorted(feature_file_dct.keys()):
            result_line += (','+lines[feature_name].split('\t')[2])
        if label == '0':
            label = 'no'
        else:
            label = 'yes'
        result_line += (','+label+'\n')
        result_file.write(result_line)
        rows.pop(0)
    idx+=1
result_file.close()